package edu.com.wjt219971332.dao;

import edu.com.wjt219971332.entity.Competition;
import org.apache.ibatis.annotations.*;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface CompetitionDao {
    @Select("SELECT * FROM competition WHERE competitionid = #{competitionid}")
    Competition queryById(Integer competitionid);

    @Select("SELECT * FROM competition LIMIT #{pageable.offset}, #{pageable.pageSize}")
    List<Competition> queryAllByLimit(Competition competition, @Param("pageable") Pageable pageable);

    @Select(" select count(1) from competition")
    long count(Competition competition);

    @Insert("INSERT INTO competition (theme, description, starttime, endtime, asinger, bsinger, status) VALUES (#{theme} ,#{description}, NOW(), #{endtime}, #{asinger}, #{bsinger}, 0)")
    @Options(useGeneratedKeys = true, keyProperty = "competitionid")
    int insert(Competition competition);


    int insertBatch(@Param("entities") List<Competition> entities);


    int insertOrUpdateBatch(@Param("entities") List<Competition> entities);

    @Update("UPDATE competition SET theme = #{theme}, description = #{description}, endtime = #{endtime}, asinger = #{asinger}, bsinger = #{bsinger}, ajudge = #{ajudge}, bjudge = #{bjudge}, ascore = #{ascore}, bscore = #{bscore}, status = #{status}, auser = #{auser}, buser = #{buser} WHERE competitionid = #{competitionid}")
    int update(Competition competition);

    @Delete("DELETE FROM competition WHERE competitionid = #{competitionid}")
    int deleteById(Integer competitionid);

    @Select("SELECT * FROM competition WHERE asinger LIKE CONCAT('%', #{asinger}, '%') OR bsinger LIKE CONCAT('%', #{asinger}, '%')")
    List<Competition> queryByusername(String asinger);
}

